package cn.regexp.elasticjob.job;

import cn.regexp.elasticjob.domain.FileCustom;
import cn.regexp.elasticjob.mapper.FileCustomMapper;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FileCustomElasticJob implements SimpleJob {

    @Autowired
    private FileCustomMapper fileCustomMapper;

    @Override
    public void execute(ShardingContext shardingContext) {
        System.out.println(shardingContext.getShardingParameter());
        doWork(shardingContext.getShardingParameter());
    }

    private void doWork(String type) {
        List<FileCustom> fileCustoms = fileCustomMapper.selectByBackupStatusAndType(type);
        System.out.println("需要备份的文件个数：" + fileCustoms.size());
        for (FileCustom fileCustom : fileCustoms) {
            try {
                // 模拟备份操作
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("备份文件的类型为：" + fileCustom.getType());
            fileCustomMapper.changeStatus(fileCustom.getId());
        }
    }

    private void doWork_bak() {
        List<FileCustom> fileCustoms = fileCustomMapper.selectByBackupStatus();
        System.out.println("需要备份的文件个数：" + fileCustoms.size());
        for (FileCustom fileCustom : fileCustoms) {
            try {
                // 模拟备份操作
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("备份文件的类型为：" + fileCustom.getType());
            fileCustomMapper.changeStatus(fileCustom.getId());
        }
    }
}
